package com.markerhub.mapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.vo.StudentVO;
import com.markerhub.entity.ManagerClass;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface ManagerClassMapper extends BaseMapper<ManagerClass> {

    @Select("SELECT COUNT(*) FROM m_course WHERE teacher_id = #{teacherId}")
    Integer countByTeacherId(@Param("teacherId") Long teacherId);

    @Select("SELECT s.* FROM m_manager_student s " +
            "JOIN m_teaching_student_in_class sc ON s.id = sc.student_id " +
            "WHERE sc.class_id = #{classId}")
    List<StudentVO> getClassStudents(@Param("classId") Long classId);
    // 自定义查询：根据多个条件查询教学班的数量
    @Select({
            "SELECT COUNT(1) "+
            "FROM m_manager_class c "+
            "LEFT JOIN m_manager_activity a ON c.activity_id = a.id "+
            "LEFT JOIN m_manager_teacher t ON c.teacher_id = t.id "+
            "WHERE 1 = 1 "+
            "AND (#{activityName} IS NULL OR a.name LIKE CONCAT('%', #{activityName}, '%')) "+
            "AND (#{className} IS NULL OR c.name LIKE CONCAT('%', #{className}, '%')) "+
            "AND (#{classDesc} IS NULL OR c.class_desc LIKE CONCAT('%', #{classDesc}, '%')) "+
            "AND (#{teacherName} IS NULL OR t.teacher_name LIKE CONCAT('%', #{teacherName}, '%'))"
    })
    Integer countClasses(
            @Param("activityName") String activityName,
            @Param("className") String className,
            @Param("classDesc") String classDesc,
            @Param("teacherName") String teacherName
    );


    // 自定义分页查询：根据多个条件查询教学班列表
    @Select({
            "SELECT c.*, a.name AS activity_name, t.teacher_name "+
            "FROM m_manager_class c "+
            "LEFT JOIN m_manager_activity a ON c.activity_id = a.id "+
            "LEFT JOIN m_manager_teacher t ON c.teacher_id = t.id "+
            "WHERE 1 = 1 "+
            "AND (#{activityName} IS NULL OR a.name LIKE CONCAT('%', #{activityName}, '%')) "+
            "AND (#{className} IS NULL OR c.name LIKE CONCAT('%', #{className}, '%')) "+
            "AND (#{classDesc} IS NULL OR c.class_desc LIKE CONCAT('%', #{classDesc}, '%')) "+
            "AND (#{teacherName} IS NULL OR t.teacher_name LIKE CONCAT('%', #{teacherName}, '%')) "+
            "ORDER BY c.id DESC"
    })
    IPage<ManagerClass> getClasses(Page<ManagerClass> page,
                                   @Param("activityName") String activityName,
                                   @Param("className") String className,
                                   @Param("classDesc") String classDesc,
                                   @Param("teacherName") String teacherName
    );


    // 自定义SQL查询：根据活动名称查询教学班的数量
    @Select("SELECT COUNT(1) FROM m_manager_class c " +
            "LEFT JOIN m_manager_activity a ON c.activity_id = a.id " +
            "WHERE a.name LIKE #{activityName}")
    Integer countClassesByActivityName(String activityName);

    // 自定义分页查询：查询符合条件的教学班列表
    @Select("SELECT c.*, a.name AS activity_name " +
            "FROM m_manager_class c " +
            "LEFT JOIN m_manager_activity a ON c.activity_id = a.id " +
            "WHERE a.name LIKE #{activityName} " +
            "ORDER BY c.id DESC")
    IPage<ManagerClass> getClassesByActivityName(Page<ManagerClass> page, String activityName);
}
